/*
 * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.dick.base.push.huawei.service;

import com.dick.base.push.huawei.android.AndroidNotification;
import com.dick.base.push.huawei.android.BadgeNotification;
import com.dick.base.push.huawei.android.ClickAction;
import com.dick.base.push.huawei.exception.HuaweiMesssagingException;
import com.dick.base.push.huawei.message.AndroidConfig;
import com.dick.base.push.huawei.message.Message;
import com.dick.base.push.huawei.message.Notification;
import com.dick.base.push.huawei.messaging.HuaweiApp;
import com.dick.base.push.huawei.messaging.HuaweiMessaging;
import com.dick.base.push.huawei.model.Importance;
import com.dick.base.push.huawei.model.Urgency;
import com.dick.base.push.huawei.model.Visibility;
import com.dick.base.push.huawei.reponse.SendResponse;
import com.dick.base.push.huawei.util.InitAppUtils;
import org.springframework.stereotype.Service;

@Service
public class SendNotifyMessageService {

    /**
     * send notification message
     *
     * @throws HuaweiMesssagingException
     */
    public SendResponse sendNotification(String topic,String customerName,String projectName,String batchNumber,int orderItemCount,String material,String thickness,String width) throws HuaweiMesssagingException {
        HuaweiApp app = InitAppUtils.initializeApp();
        HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app);

        Notification notification = Notification.builder().setTitle("客户：".concat(customerName))
                .setBody(projectName.concat(batchNumber).concat("，共").concat(orderItemCount+"").concat("个订单项，材质：")
                        .concat(material).concat("，厚度：").concat(thickness).concat("，宽度：").concat(width))
                .build();


        AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")
                .setTag("new_order")
                .setClickAction(ClickAction.builder().setType(1).setIntent("pushscheme://com.mozi.modbus/main").build())
                .setChannelId("new_order")
                .setNotifySummary("新订单提示")
                .setStyle(0)
                .setAutoClear(86400000)
                .setNotifyId(486)
                .setGroup("new_order".concat(System.currentTimeMillis()+""))
                .setImportance(Importance.HIGH.getValue())
                //.setBadge(BadgeNotification.builder().setAddNum(1).setBadgeClass("com.mozi.modbus.module.user.login.LoginActivity").build())
                .setVisibility(Visibility.PUBLIC.getValue())
                .setForegroundShow(false)
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1)
                .setUrgency(Urgency.HIGH.getValue())
                .setTtl("10000s")
                .setBiTag("the_sample_bi_tag_for_receipt_service")
                .setNotification(androidNotification)
                .build();

        Message message = Message.builder().setNotification(notification)
                .setAndroidConfig(androidConfig)
                .setTopic(topic)
                .build();

        return huaweiMessaging.sendMessage(message);
    }
}
